package game.handler.implement;

import java.awt.event.KeyEvent;
import java.util.HashMap;

import game.classes.gameField.gameArea;
import game.classes.gameField.gamePlayer;
import game.classes.interfaces.Block;
import game.classes.interfaces.Player;
import game.classes.interfaces.Tile;
import game.clock.gameClock;
import game.enums.Direction;
import game.enums.KeyID;
import game.enums.MenuType;
import game.handler.interfaces.GameController;
import game.renderer.Denis.Renderer;

public class gameGameController implements GameController{
	
	private gameKeyboardHandler keyboardHandler;
	private gameArea area;
	private gameClock clock;
	private Renderer renderer;
	private HashMap<KeyID,gameKeyState> keyPressMap;
	private gamePlayer player;

	public void setGameArea(gameArea Area)
	{
		this.area=Area;
	}
	
	public gameGameController(gamePlayer play){
		keyPressMap=gameKeyboardHandler.createKeyPressMap();
		clock = new gameClock(this);
		keyboardHandler = new gameKeyboardHandler(keyPressMap,gameKeyboardHandler.createDefaultKeyMap());
		player = play;
	}
	
	public void startGame(){
		clock.run();
	}
	
	public void setRenderer(Renderer renderer)
	{
		this.renderer=renderer;	
	}
	
	@Override
	public void killPlayer() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void respawnPlayer() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Block[][] getBlockList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Tile[][] getTileList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Player getPlayer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MenuType getMenuType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void keyPressed(KeyEvent key) {
		keyboardHandler.handleKeyEvent(key);
		
	}

	@Override
	public void keyReleased(KeyEvent key) {
		keyboardHandler.handleKeyEvent(key);
	}

	private int x=2,y=2;
	@Override
	public void tickAll() {
		int waittick=3;
		for (gameKeyState a:keyPressMap.values()){
			if (a.keyDown) a.keyTicks++; else a.keyTicks=0;
		}
		if (keyPressMap.get(KeyID.UP).keyTicks>waittick){ 
			if (area.moveBlock(x, y, Direction.UP)) {
				y--;
			}
			keyPressMap.get(KeyID.UP).keyTicks=0;
		}
		if (keyPressMap.get(KeyID.DOWN).keyTicks>waittick){ 
			if (area.moveBlock(x, y, Direction.DOWN)) {
				y++;
			}
			keyPressMap.get(KeyID.DOWN).keyTicks=0;
		}
		if (keyPressMap.get(KeyID.LEFT).keyTicks>waittick){ 
			if (area.moveBlock(x, y, Direction.LEFT)) {
				x--;
			}
			keyPressMap.get(KeyID.LEFT).keyTicks=0;
		}
		if (keyPressMap.get(KeyID.RIGHT).keyTicks>waittick){ 
			if (area.moveBlock(x, y, Direction.RIGHT)) {
				x++;
			}
			keyPressMap.get(KeyID.RIGHT).keyTicks=0;
		}
		renderer.paint(((Integer) clock.getFPS()).toString());
	}

}
